﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
using System.Configuration;
using CommonData;

namespace DataCentral.Forms
{
    public partial class SQLConnectionForm : CommonChildForm
    {
        public SQLConnectionForm()
        {
            InitializeComponent();
        }

        string GetServerConnectionString()
        {
            string connectionString = String.Format("data source={0};initial catalog= {1};integrated security=SSPI", txtServer.Text,txtDatabase.Text);
            if (rdgConnect.SelectedIndex == 1)
                connectionString = String.Format("data source={0};initial catalog= {1};user id={2};password={3}", txtServer.Text, txtDatabase.Text, txtLogin.Text, txtPassword.Text);
            return connectionString;
        }

        private void SQLConnectionForm_Load(object sender, EventArgs e)
        {
            txtServer.Text = Properties.Settings.Default.APP_Datasource;
            txtDatabase.Text = Properties.Settings.Default.APP_InitCatalog;
            rdgConnect.SelectedIndex = Properties.Settings.Default.APP_IntegratedSecurity ? 0 : 1;
            txtLogin.Text = Properties.Settings.Default.APP_ServerID;
            txtPassword.Text = CommonFunctions.DecodeFrom64(Properties.Settings.Default.APP_ServerPassword);
        }

        private void rdgConnect_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool disable = rdgConnect.SelectedIndex == 0;
            DisableSQLServerAuthentication(disable);
        }

        void DisableSQLServerAuthentication(bool disable)
        {
            txtLogin.Enabled = !disable;
            txtPassword.Enabled = !disable;
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            using (SqlConnection connection = new SqlConnection(GetServerConnectionString()))
            {
                try
                {
                    connection.Open();
                    MessageClass.ShowInformationMessage("Kết nối cơ sở dữ liệu thành công với thông tin được cấu hình.");
                }
                catch
                {
                    MessageClass.ShowErrorMessage("Kết nối cơ sở dữ liệu không thành công.");
                    return;
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.APP_Datasource = txtServer.Text.Trim();
            Properties.Settings.Default.APP_InitCatalog = txtDatabase.Text.Trim();
            Properties.Settings.Default.APP_IntegratedSecurity = rdgConnect.SelectedIndex == 0;
            Properties.Settings.Default.APP_ServerID = txtLogin.Text.Trim();
            Properties.Settings.Default.APP_ServerPassword = CommonFunctions.EncodeTo64(txtPassword.Text.Trim());
            Properties.Settings.Default.Save();
            Properties.Settings.Default.Reload();

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.ConnectionStrings.ConnectionStrings["DataCentral.Properties.Settings.DataCentralConnectionString"].ConnectionString = GetServerConnectionString();
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("connectionStrings");
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}